package com.example.campuslink.ui.activity

import android.text.Editable
import android.text.TextWatcher
import android.view.View
import android.widget.Toast
import androidx.lifecycle.viewModelScope
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.campuslink.BR
import com.example.campuslink.R
import com.example.campuslink.commen.adpter.CallUserListAdapter
import com.example.campuslink.commen.adpter.PublicPhotoAdapter
import com.example.campuslink.commen.dialog.TopicDialog
import com.example.campuslink.commen.tools.SpacesItemEndDecoration
import com.example.campuslink.commen.tools.putExtraKey
import com.example.campuslink.databinding.ActivityPublicBinding
import com.example.campuslink.viewModel.PublicViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class PublicActivity : BaseActivity<ActivityPublicBinding, PublicViewModel>(
    PublicViewModel::class.java,
    R.layout.activity_public,
    BR.viewModel
) {
    private var publicPhotoAdapter: PublicPhotoAdapter? = null
    private var currentTopic = ""
    private var callUserListAdapter: CallUserListAdapter? = null
    private var callUser: String = ""
    override fun initView() {
        mViewModel?.context = this
        mViewModel?.initDB(this)
        initData()
        onClick()
        mViewModel?.getSchoolUserNumber()
        mViewModel?.isShowCallUserNumber?.observe(this) {
            if (it.length > 0) {
                mViewDataBinding?.etCallUserNumber?.visibility = View.VISIBLE
                if (it != callUser) {
                    callUser = it
                    mViewDataBinding?.etCallUserNumber?.post {
                        mViewDataBinding?.etCallUserNumber?.setText(it)
                    }
                }

            } else {
                mViewDataBinding?.etCallUserNumber?.visibility = View.GONE
            }
        }

        mViewModel?.topicList?.observe(this) {
            if (it.isNotEmpty()) {
                mViewDataBinding?.etInvitationType?.visibility = View.VISIBLE
            }
            if (currentTopic != it) {
                currentTopic = it
                mViewDataBinding?.etInvitationType?.post {
                    mViewDataBinding?.etInvitationType?.setText(it)
                }
            }
        }
        mViewModel?.isShowUserList?.observe(this) {
            if (it) {
                mViewModel?.getSchoolUserNumber()
                mViewDataBinding?.rvUserList?.visibility = View.VISIBLE
            } else {
                mViewDataBinding?.rvUserList?.visibility = View.GONE
            }
        }

        mViewModel?.userList?.observe(this) {
            it.forEach { item ->
                mViewModel?.getUserList(item)
            }
        }

        mViewModel?.userInfoList?.observe(this) {
            callUserListAdapter?.setListData(it)
        }
    }


    fun addTopic(topic: String) {
        mViewModel?.addTopic(topic)
    }


    fun onClick() {
        mViewDataBinding?.tvPublic?.setOnClickListener({
            if (mViewDataBinding?.etTitle?.text?.toString()
                    ?.isNotEmpty()!! && mViewDataBinding?.etContent?.text?.toString()
                    ?.isNotEmpty()!!
            ) {
                mViewModel?.publicInvitation(
                    mViewDataBinding?.etTitle?.text?.toString() ?: "",
                    mViewDataBinding?.etContent?.text?.toString() ?: ""
                )
            } else {
                Toast.makeText(this, "请填写标题和内容", Toast.LENGTH_SHORT).show()
            }
        })
        mViewDataBinding?.btnCallUser?.setOnClickListener({
            if (mViewModel?.userList?.value?.size == 0) {
                Toast.makeText(this, "该学校暂无其他用户!", Toast.LENGTH_SHORT).show()
            } else {
                mViewModel?.setIsShowUserList()
            }

        })
        mViewDataBinding?.ivClose?.setOnClickListener({
            finish()
        })
        mViewDataBinding?.btnTopic?.setOnClickListener({
            val topic = TopicDialog()
            topic.show(supportFragmentManager, "TopicDialog")
        })
        mViewDataBinding?.etCallUserNumber?.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun afterTextChanged(p0: Editable?) {
                mViewModel?.setIsShowCallUserNumber(p0.toString())
                mViewDataBinding?.etCallUserNumber?.setSelection(p0?.length ?: 0)
            }

        })
        mViewDataBinding?.etInvitationType?.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {

            }

            override fun afterTextChanged(p0: Editable?) {
                mViewModel?.setTopic(p0.toString())
                mViewDataBinding?.etInvitationType?.setSelection(p0?.length ?: 0)
            }

        })
    }

    fun initData() {
        val isFromPhoto = intent.getBooleanExtra(putExtraKey.ISFROM_PHOTO.key, true)
        val imagePath = intent.getStringExtra(putExtraKey.IMAGE_PATH.key)
        val allFilePath = intent.getSerializableExtra(putExtraKey.ALL_FILE_PATH.key).toString()

        callUserListAdapter = CallUserListAdapter(mViewModel!!)
        publicPhotoAdapter = PublicPhotoAdapter(mViewModel!!)
        mViewDataBinding?.rvUserList?.adapter = callUserListAdapter
        mViewDataBinding?.rvUserList?.layoutManager =
            LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
        mViewDataBinding?.rvUserList?.addItemDecoration(SpacesItemEndDecoration(15))
        mViewDataBinding?.rvImg?.adapter = publicPhotoAdapter
        mViewDataBinding?.rvImg?.addItemDecoration(SpacesItemEndDecoration(15))
        mViewDataBinding?.rvImg?.layoutManager =
            LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
        if (isFromPhoto) {
            publicPhotoAdapter?.setListData(
                allFilePath.replace("[", "").replace("]", "").replace(" ", "").split(",")
            )
            mViewModel?.allImagePath =
                allFilePath.replace("[", "").replace("]", "").replace(" ", "").split(",")
                    .toMutableList()
        } else {
            publicPhotoAdapter?.setListData(listOf(imagePath.toString()))
            mViewModel?.allImagePath = mutableListOf(imagePath.toString())
        }
    }
}